using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace QUAN_LY_NGAN_HANG_CAU_HOI_TRAC_NGHIEM
{
    public class DapAnDAL
    {
        private Guid _maSoCauHoi;
        public Guid MaSoCauHoi
        {
            get { return _maSoCauHoi; }
            set { _maSoCauHoi = value; }            
        }

        private int _sTTDapAn;
        public int STTDapAn
        {
            get { return _sTTDapAn; }
            set { _sTTDapAn = value; }
        }

        private String _noiDungDapAn;
        public String NoiDungDapAn
        {
            get { return _noiDungDapAn; }
            set { _noiDungDapAn = value; }
        }

        private bool _dapAnDung;
        public bool DapAnDung
        {
            get { return _dapAnDung; }
            set { _dapAnDung = value; }
        }

        public static void InsertDapAn(DapAnDAL dapan)
        {
            using (SqlConnection cn = new SqlConnection(Globals.ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("usp_InsertDapAn", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@MaCauHoi", SqlDbType.UniqueIdentifier).Value = dapan.MaSoCauHoi;
                cmd.Parameters.Add("@NoiDungDapAn", SqlDbType.NVarChar, 250).Value = dapan.NoiDungDapAn;
                cmd.Parameters.Add("@STTDapAn", SqlDbType.Int).Value = dapan.STTDapAn;
                cmd.Parameters.Add("@DapAnDung", SqlDbType.Bit).Value = dapan.DapAnDung;
                cn.Open();
                cmd.ExecuteNonQuery();
            }
        }

       public static List<DapAnDAL> LayDapAnTheoCauHoi(Guid macauhoi)
        {
           using (SqlConnection cn = new SqlConnection(Globals.ConnectionString))
           {
               List<DapAnDAL> lst = new List<DapAnDAL>();
               SqlCommand cmd = new SqlCommand("usp_LayDapAnTheoCauHoi", cn);
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.Add("@MaSoCauHoi", SqlDbType.UniqueIdentifier).Value = macauhoi;
               cn.Open();
               IDataReader rd = cmd.ExecuteReader();
               if (rd != null)
               {
                   while (rd.Read())
                   {
                       DapAnDAL en = new DapAnDAL();
                       en.MaSoCauHoi = (Guid) rd["MaSoCauHoi"];
                       en.NoiDungDapAn = (string) rd["NoiDungDapAn"];
                       en.STTDapAn = (int) rd["STTDapAn"];
                       en.DapAnDung = (bool) rd["DapAnDung"];
                       lst.Add(en);
                   }
               }
               return lst;
           }
        }
    }
}